home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C11 / Reference.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  588 b   |  30 lines

  1. //: C11:Reference.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Simple C++ references
  7.  
  8. int* f(int* x) {
  9.   (*x)++;
  10.   return x; // Safe; x is outside this scope
  11. }
  12.  
  13. int& g(int& x) {
  14.   x++; // Same effect as in f()
  15.   return x; // Safe; outside this scope
  16. }
  17.  
  18. int& h() {
  19.   int q;
  20. //!  return q;  // Error
  21.   static int x;
  22.   return x; // Safe; x lives outside scope
  23. }
  24.  
  25. int main() {
  26.   int a = 0;
  27.   f(&a); // Ugly (but explicit)
  28.   g(a);  // Clean (but hidden)
  29. } ///:~
  30.